package net.hmr_radio.hmrbot.model;

import java.io.Serializable;
import java.util.Date;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.slim3.datastore.Attribute;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

@Model(schemaVersion = 1)
public class Bot implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(persistent = false)
    private Log log = LogFactory.getLog(Bot.class);

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    private String name;

    private Text description;

    /** twitter screen name. */
    private String twitterId;

    private String accessToken;

    private String accessTokenSecret;

    private Date lastRun;

    private Boolean stop;

    /** owner's userId. */
    private String ownerId;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        result = prime * result + ((version == null) ? 0 : version.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Bot other = (Bot) obj;
        if (key == null) {
            if (other.key != null)
                return false;
        } else if (!key.equals(other.key))
            return false;
        if (version == null) {
            if (other.version != null)
                return false;
        } else if (!version.equals(other.version))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Bot [name=" + name + "]";
    }

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Text getDescription() {
        return description;
    }

    public void setDescription(Text description) {
        this.description = description;
    }

    public String getTwitterId() {
        return twitterId;
    }

    public void setTwitterId(String twitterId) {
        this.twitterId = twitterId;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getAccessTokenSecret() {
        return accessTokenSecret;
    }

    public void setAccessTokenSecret(String accessTokenSecret) {
        this.accessTokenSecret = accessTokenSecret;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public Date getLastRun() {
        return lastRun;
    }

    public void setLastRun(Date lastRun) {
        this.lastRun = lastRun;
    }

    public Boolean getStop() {
        return stop;
    }

    public void setStop(Boolean stop) {
        this.stop = stop;
    }

    public Twitter createTwitterOrNull() {
        try {
            if (getAccessToken() != null && getAccessTokenSecret() != null) {
                Twitter twitter = TwitterFactory.getSingleton();
                twitter.setOAuthAccessToken(new AccessToken(
                    getAccessToken(),
                    getAccessTokenSecret()));
                twitter.getHomeTimeline();
                return twitter;
            }
        } catch (TwitterException e) {
            log.warn("Twitterオブジェクト生成エラー", e);
        }
        return null;
    }

    public Key createKey() throws NullPointerException {
        if (twitterId == null) {
            throw new NullPointerException("twitterId is null");
        }
        return Datastore.createKey(Bot.class, twitterId);
    }
}
